package com.example.ioc.context;

import com.example.common.utils.Statistics;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * {@link Component}在
 * {@link org.springframework.context.annotation.ConfigurationClassPostProcessor}<br>
 * 中被处理.
 * 可以debug {@link org.springframework.context.annotation.ConfigurationClassUtils#isConfigurationCandidate(AnnotationMetadata)}
 * 
 * {@link ConfigurableApplicationContext#addBeanFactoryPostProcessor(BeanFactoryPostProcessor)}
 * {@link org.springframework.context.ApplicationContextInitializer#initialize(ConfigurableApplicationContext)}
 */
@Slf4j
//@Component //可以通过Component注入
public class LBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor, PriorityOrdered {
    
    private static volatile LBeanDefinitionRegistryPostProcessor instance;
    
    private LBeanDefinitionRegistryPostProcessor(){
        Statistics.logInfo(log);
    }
    
    public static LBeanDefinitionRegistryPostProcessor getInstance(){
        if(instance == null){
            synchronized (LBeanDefinitionRegistryPostProcessor.class){
                if(instance == null){
                    instance = new LBeanDefinitionRegistryPostProcessor();
                }
            }
        }
        return instance;
    }

    @Override
    public void postProcessBeanDefinitionRegistry(@NonNull BeanDefinitionRegistry registry) throws BeansException {
//        BeanDefinition beanDefinition = new AnnotatedGenericBeanDefinition(LBean.class);
//        beanDefinition.setLazyInit(true);
//        beanDefinition.setScope(LScope.SCOPE_NAME);
//        registry.registerBeanDefinition(beanDefinition.getBeanClassName(), beanDefinition);
        Statistics.logInfo(log, "postProcessBeanDefinitionRegistry", registry);
    }

    @Override
    public void postProcessBeanFactory(@NonNull ConfigurableListableBeanFactory beanFactory) throws BeansException {
        Statistics.logInfo(log, "postProcessBeanFactory", beanFactory);
    }

    /**
     * {@link org.springframework.context.support.PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory, List)}
     * 中有个逻辑，实现{@link PriorityOrdered}的BeanDefinitionRegistryPostProcessor,优先调用
     */
    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE;
    }
}
